package com.xiaotu.spider.microblog.pageprocessor;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.TVWeiboModel;
import com.xiaotu.common.util.CacheHandler;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.HtmlUtil;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.spider.APageProcessor;
import com.xiaotu.spider.downloader.SpiderHttpClientDownloader;

import net.sf.json.JSONObject;

/**
 * @类名 WeiboPageProcessor
 * @日期 2017年2月15日
 * @作者 高海军
 * @功能 作品官微数据爬取
 */
@Component("TVMicBlogPageProcessor")
@Scope("prototype")
public class TVMicBlogPageProcessor extends APageProcessor
{
	private static final Logger LOGGER = LoggerFactory
			.getLogger(TVMicBlogPageProcessor.class);
	
	@Autowired
	protected CacheHandler cacheHandler;
	
	@Override
	protected Request getStartRequest(JSONObject target)
	{
		if (!target.has("weibourl")
				|| StringUtils.isEmpty(target.getString("weibourl"))
				|| !RegexUtils.regexMatch(RegexUtils.REGEX_URL,
						target.getString("weibourl")))
		{
			LOGGER.warn("[{}] Micro Blog Url is error:{}",
					spiderLog.getJobType(), target.toString());
			return null;
		}
		String url = target.getString("weibourl").replace(".com", ".cn");
		return new Request(url);
	}
	
	@Override
	protected String getSpiderType()
	{
		return Constants.SpiderTaskType.TV_MICROBLOG;
	}
	
	@Override
	public String getPageDataType(Page page)
	{
		return Constants.PageDataType.MICRO_BLOG;
	}
	
	@Override
	public void process(Page page)
	{
		String[] data;
		try
		{
			data = this.getPageData(page);
		}
		catch (SpiderException e)
		{
			if (page.getHtml().toString().contains("是否采集设备指纹"))// 说明需要身份验证，故放入补爬任务
			{
				page.getRequest().putExtra(
						SpiderHttpClientDownloader.IOEXCEPTION_KEY,
						"Must Login");
				this.downloader.addCorrectCache(page.getRequest());
			}
			throw e;
		}
		
		Map<String, Object> tvMap = page.getRequest().getExtras();
		page.putField(TVWeiboModel.class.getSimpleName(),
				new TVWeiboModel(tvMap.get("tvid") + "",
						tvMap.get("tvName") + "", data[1], null, null, data[2],
						data[0], "1"));
		
	}
	
	protected String[] getPageData(Page page)
	{
		String html = page.getHtml().toString();
		
		List<String> labelList = RegexUtils.regexSearchAll(
				"<strong class=\\\\\"(.*?)\\\\\">(.*)<\\\\/strong><span class=\\\\\"(.*?)\\\\\">关注<\\\\/span>|"
						+ "<strong class=\\\\\"(.*?)\\\\\">(.*)<\\\\/strong><span class=\\\\\"(.*?)\\\\\">粉丝<\\\\/span>|"
						+ "<strong class=\\\\\"(.*?)\\\\\">(.*)<\\\\/strong><span class=\\\\\"(.*?)\\\\\">微博<\\\\/span>",
				html);
		
		if (labelList == null || labelList.isEmpty())
			return this.getMicroBlogByOther(html);
		
		String concern = null, fans = null, weibo = null;
		
		for (String label : labelList)
		{
			List<String> valueList = RegexUtils.regexSearchAll(
					"<strong class=\\\\\"(.*?)\\\\\">(.*)<\\\\/strong>|<span class=\\\\\"(.*?)\\\\\">(.*)<\\\\/span>",
					label);
			if (valueList == null || valueList.size() < 2)
				continue;
			String value = HtmlUtil.delHTMLTag(valueList.get(0));
			String tag = HtmlUtil.delHTMLTag(valueList.get(1));
			
			switch (tag)
			{
				case "关注":
					concern = value;
					break;
				case "粉丝":
					fans = value;
					break;
				case "微博":
					weibo = value;
					break;
			}
		}
		return this.getTagValue(concern, fans, weibo);
	}
	
	/**
	 * 获取页面上的微博数据（改页面格式不是常规格式，只有粉丝和关注，没有微博）
	 * @param html 页面内容发
	 */
	private String[] getMicroBlogByOther(String html)
	{
		// List<String> labelList = RegexUtils.regexSearchAll(
		// "他的关注\\(" + RegexUtils.REGEX_INTEGER + "\\)|他的粉丝\\("
		// + RegexUtils.REGEX_INTEGER + "\\)|她的关注\\("
		// + RegexUtils.REGEX_INTEGER + "\\)|她的粉丝\\("
		// + RegexUtils.REGEX_INTEGER + "\\)",
		// html);
		
		List<String> labelList = RegexUtils
				.regexSearchAll("微博\\[" + RegexUtils.REGEX_INTEGER + "\\]|粉丝\\["
						+ RegexUtils.REGEX_INTEGER + "\\]|关注\\["
						+ RegexUtils.REGEX_INTEGER + "\\]", html);
		
		if (labelList == null || labelList.isEmpty())
		{
			LOGGER.error("[{}] html error:{}", spiderLog.getJobType(), html);
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		}
		
		String concern = null, fans = null, weibo = null;
		
		for (String label : labelList)
		{
			String tag = RegexUtils.regexSearch("微博|关注|粉丝", label);
			String value = RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER,
					label);
			
			switch (tag)
			{
				case "关注":
					concern = value;
					break;
				case "粉丝":
					fans = value;
					break;
				case "微博":
					weibo = value;
					break;
			}
			
		}
		return this.getTagValue(concern, fans, weibo);
	}
	
	private String[] getTagValue(String concern, String fans, String weibo)
	{
		if (StringUtils.isBlank(concern) && StringUtils.isBlank(fans)
				&& StringUtils.isBlank(weibo))
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		
		if (StringUtils.isNotBlank(concern)
				&& !RegexUtils.regexMatch(RegexUtils.REGEX_NUMBER, concern))
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		if (StringUtils.isNotBlank(fans)
				&& !RegexUtils.regexMatch(RegexUtils.REGEX_NUMBER, fans))
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		if (StringUtils.isNotBlank(weibo)
				&& !RegexUtils.regexMatch(RegexUtils.REGEX_NUMBER, weibo))
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		
		return new String[] { concern, fans, weibo };
	}
}
